
/*-----------------------------------------------*
Name: Haqdarshak Pilot Intervention-Analysis
Date Created: 30 July, 2018
Date Last Modified: 28 April, 2021
Created by: Aaron Berman and modified by Daniela Paz and Saumya Mathur on Nov.13/2020 
Uses Data: scheme_applications_hhlevel.dta and individual_scheme_counts.dta
Creates Data: Latex Tables
Description: Creates tables for the main analysis on schemes count
*-------------------------------------------------*/



*..................................................
**Install user-written commands
foreach package in mdesc nmissing veracrypt {
     capture which `package'
	 if _rc==111 ssc install `package'
}
*..................................................
clear all
set more off
version 12.0
cap log close
pause off



//open log 
*cd "$logs"
*log using "tables_3_4.smcl", replace



*Table 4 and Appendix Table 6

use "$intermediate_data/scheme_applications_hhlevel", clear

	****MAIN SCHEME COUNTS****
	//count each scheme
	matrix schemecounts = J(8, 5, .)
	matrix rownames schemecounts = suk savings ujjwala awas oldage palanhaar widow atal 

	forv i = 1/8 {
		local schemetitle: word `i' of `""Sukanya Samriddhi Account Scheme_RJ""' `""Savings Account_RJ""' `""Pradhan Mantri Ujjwala Yojana_RJ""' ///
			`""Pradhan Mantri Awas Yojana- Gramin_RJ""' `""Chief Ministers Old Age Pension Scheme""' `""Palanhaar""' `""Chief Ministers Widow Pension Scheme""' ///
			`""Atal Pension Yojana_RJ""' 

		if `schemetitle' == "Palanhaar" {
			qui count if scheme_id == 6
			local palanhaar_offered = `r(N)'
			matrix schemecounts[6,1] = `r(N)'
			qui count if scheme_id == 6 & atleast1_agreed_hh == 1
			matrix schemecounts[6,2] = `r(N)'
			qui count if scheme_id == 6 & atleast1_applied_hh == 1
			local palanhaar_applied = `r(N)'
			matrix schemecounts[6,3] = `r(N)'
			local pct: di %8.1f (`palanhaar_applied'/`palanhaar_offered')*100
			matrix schemecounts[6,4] = `pct'
			matrix schemecounts[6,5] = 6
		}
		else {
			qui count if scheme_id == `i' 
			local offered_`i' = `r(N)'
			matrix schemecounts[`i',1] = `r(N)'
			qui count if scheme_id == `i' & atleast1_agreed_hh == 1
			matrix schemecounts[`i',2] = `r(N)'
			qui count if scheme_id == `i' & atleast1_applied_hh == 1
			local applied_`i' = `r(N)'
			matrix schemecounts[`i',3] = `r(N)'
			local pct: di %8.1f (`applied_`i'' / `offered_`i'')*100
			matrix schemecounts[`i',4] = `pct'
			matrix schemecounts[`i',5] = `i'
		}
	}

	//sort matrix by number of counts 
	mata : st_matrix("schemecounts", sort(st_matrix("schemecounts"), -1))
	local rownames = "suk savings ujjwala awas oldage palanhaar widow atal"
	tokenize `rownames'
	local newrownames
	forv i = 1/8 {
		local add = schemecounts[`i',5]
		local name ``add''
		local newrownames: list newrownames | name
	}

	matrix schemecounts_final = J(8, 4, .)
	forv i = 1/8 {
		forv j = 1/4 {
			matrix schemecounts_final[`i',`j'] = schemecounts[`i',`j']
		}
	}


	matrix rownames schemecounts_final = `newrownames'

	//output table
	cd "$Main"
	#delimit ;
	esttab matrix(schemecounts_final) using "Table_5.tex",
		label booktabs
		collabels("HHs Offered" "HHs Agreed" "HHs Assisted" "Assistance /% of Offered")
		varlabels(suk "Female Child Savings Account (Sukanya Samriddhi)" savings "Post Office Savings Account" ujjwala "LPG Gas Connection (PM Ujjwala Yojana)" ///
			awas "Home-Building Assistance (PM Awas Yojana)" oldage "Chief Ministers Old Age Pension Scheme" palanhaar "Child Support (Palanhaar) Schemes" widow "Chief Ministers Widow Pension Scheme" ///
			atal "Social Security Pension (Atal Pension Yojana)")
		mlabels(none)
		replace;
	#delimit cr
	pause


	****INFORMATION SCHEME COUNTS****
	//count each scheme
	matrix schemecounts = J(6, 5, .)
	matrix rownames schemecounts = janani mukhya surakshit shishu iron pradhan 

	forv i = 9/14 {
		local schemetitle: word `i' of `""Janani Suraksha Yojana_RJ""' `""Mukhya Mantri Rajshree Yojana_RJ""' `""Pradhan Mantri Surakshit Mstritva Abhiyan (PMSMA)_RJ""' `""Janani Shishu Suraksha Karyakram_RJ""' ///
			`""Iron and folic acid supplementation scheme_RJ""' `""Pradhan mantri Matritva Vandana Yojana_RJ""'

			qui count if scheme_id == `i' 
			local offered_`i' = `r(N)'
			matrix schemecounts[`i'-8,1] = `r(N)'
			qui count if scheme_id == `i' & atleast1_agreed_hh == 1
			matrix schemecounts[`i'-8,2] = `r(N)'
			qui count if scheme_id == `i' & atleast1_applied_hh == 1
			local applied_`i' = `r(N)'
			matrix schemecounts[`i'-8,3] = `r(N)'
			local pct: di %8.2f `applied_`i'' / `offered_`i''
			matrix schemecounts[`i'-8,4] = `pct'
			matrix schemecounts[`i'-8,5] = `i'
	}

	//sort matrix by number of counts 
	mata : st_matrix("schemecounts", sort(st_matrix("schemecounts"), -1))
	local rownames = "janani mukhya surakshit shishu iron pradhan"
	tokenize `rownames'
	local newrownames
	forv i = 1/6 {
		local add = schemecounts[`i',5] - 8
		local name ``add''
		local newrownames: list newrownames | name
	}

	matrix schemecounts_final = J(6, 2, .)
	forv i = 1/6 {
		forv j = 1/2 {
			matrix schemecounts_final[`i',`j'] = schemecounts[`i',`j']
		}
	}



	matrix rownames schemecounts_final = `newrownames'

	//output table
	cd "$Appe"
	#delimit ;
	esttab matrix(schemecounts_final) using "Table_A20.tex",
		label booktabs
		collabels("HHs Offered" "HHs Agreed")
		varlabels(janani "Janani Suraksha Yojana" mukhya "Mukhya Mantri Rajshree Yojana" surakshit "Pradhan Mantri Surakshit Mstritva Abhiyan (PMSMA)" ///
			shishu "Janani Shishu Suraksha Karyakram" iron "Iron and Folic Acid Supplementation" pradhan "Pradhan Mantri Matritva Vandana Yojana")
		mlabels(none)
		replace;
	#delimit cr




*Table 3 and Appendix Table 5

	use "$intermediate_data/individual_scheme_counts", clear
	
	//count INDIVIDUAL eligibility and store in matrix 
	matrix indiv_eligibility = J(6, 3, .)
	matrix rownames indiv_eligibility = sukanya postoffice oldage_pension widow_pension awas_yojana atalpension


	local j "sukanya postoffice oldage_pension widow_pension awas_yojana atalpension"
	foreach i of numlist 1/6  {
		local x: word `i' of `j'

		count if has_elig_`x' == 1 | doesnt_have_elig_`x' == 1
		local elig_all_`x' = `r(N)'
		matrix indiv_eligibility[`i', 1] = `r(N)'

		count if has_elig_`x' == 1 & administer_module10 == 1
		local has_elig_`x' = `r(N)'
		matrix indiv_eligibility[`i', 2] = `r(N)'

		count if doesnt_have_elig_`x' == 1 & administer_module10 == 1
		local doesnt_have_elig_`x' = `r(N)'
		matrix indiv_eligibility[`i', 3] = `r(N)'

	}

	cd "$Appe"
	#delimit ;
	esttab matrix(indiv_eligibility) using "Table_A4.tex",
		label booktabs
		collabels("Eligible (All)" "Enrolled" "Not Enrolled")
		varlabels(sukanya "Female Child Account" postoffice "Post Office Savings Account" oldage_pension "Chief Ministers Old Age Pension Scheme" ///
			widow_pension "Chief Ministers Widow Pension Scheme" awas_yojana "Home-Building Assistance" atalpension "Atal Pension") 
		mlabels(none)
		replace;
	#delimit cr

	pause

	//count *household* eligibility and store in matrix 
	matrix hh_eligibility = J(6, 3, .)
	matrix rownames hh_eligibility = sukanya postoffice oldage_pension widow_pension awas_yojana atalpension

	preserve
	collapse (sum) has_elig_* doesnt_have_elig_* has_not_elig_* (mean) administer_module10, by(household_id)
	local j "sukanya postoffice oldage_pension widow_pension awas_yojana atalpension"
	foreach i of numlist 1/6  {
		local x: word `i' of `j'

		count if has_elig_`x' >= 1 | doesnt_have_elig_`x' >= 1
		matrix hh_eligibility[`i', 1] = `r(N)'

		count if has_elig_`x' >= 1 & administer_module10 == 1
		local h_has_elig_`x' = `r(N)'
		matrix hh_eligibility[`i', 2] = `r(N)'

		count if doesnt_have_elig_`x' >= 1 & has_elig_`x' == 0 & administer_module10 == 1
		local h_not_have_elig_`x' = `r(N)'
		matrix hh_eligibility[`i', 3] = `r(N)'
	}
	restore

	//output table
	cd "$Main"
	#delimit ;
	esttab matrix(hh_eligibility) using "Table_2.tex",
		label booktabs
		collabels("Eligible (All)" "Enrolled" "Not Enrolled")
		varlabels(sukanya "Female Child Savings Account (Sukanya Samriddhi)" postoffice "Post Office Savings Account" oldage_pension "Chief Ministers Old Age Pension Scheme" ///
			widow_pension "Chief Ministers Widow Pension Scheme" awas_yojana "Home-Building Assistance (PM Awas Yojana)" atalpension "Social Security Pension (Atal Pension Yojana)") 
		mlabels(none)
		replace;
	#delimit cr








